
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />

    <title>SMbHandler Class &#8212; pysmb 1.2.8 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/sphinxdoc.css" />
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
    <script src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="SMBProtocolFactory Class" href="smb_SMBProtocolFactory.html" />
    <link rel="prev" title="SMBConnection Class" href="smb_SMBConnection.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="smb_SMBProtocolFactory.html" title="SMBProtocolFactory Class"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="smb_SMBConnection.html" title="SMBConnection Class"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">pysmb 1.2.8 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">SMbHandler Class</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="smbhandler-class">
<h1>SMbHandler Class<a class="headerlink" href="#smbhandler-class" title="Permalink to this heading">¶</a></h1>
<p>The SMBHandler class provides support for “<a class="reference external" href="smb://">smb://</a>” URLs in the <a class="reference external" href="http://docs.python.org/library/urllib2.html">urllib2</a> python package.</p>
<section id="notes">
<h2>Notes<a class="headerlink" href="#notes" title="Permalink to this heading">¶</a></h2>
<ul class="simple">
<li><p>The host component of the URL must be one of the following:</p>
<ul>
<li><p>A fully-qualified hostname that can be resolved by your local DNS service. Example: myserver.test.com</p></li>
<li><p>An IP address. Example: 192.168.1.1</p></li>
<li><p>A comma-separated string “&lt;NBName&gt;,&lt;IP&gt;” where <em>&lt;NBName&gt;</em> is the Windows/NetBIOS machine name for remote SMB service, and <em>&lt;IP&gt;</em> is the service’s IP address. Example: MYSERVER,192.168.1.1</p></li>
</ul>
</li>
<li><p>The first component of the path in the URL points to the name of the shared folder.
Subsequent path components will point to the directory/folder of the file.</p></li>
<li><p>You can retrieve and upload files, but you cannot delete files/folders or create folders.
In uploads, if the parent folders do not exist, an <em>urllib2.URLError</em> will be raised.</p></li>
</ul>
</section>
<section id="example">
<h2>Example<a class="headerlink" href="#example" title="Permalink to this heading">¶</a></h2>
<p>The following code snippet illustrates file retrieval with Python 2.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">urllib2</span>
<span class="kn">from</span> <span class="nn">smb.SMBHandler</span> <span class="kn">import</span> <span class="n">SMBHandler</span>

<span class="n">director</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">SMBHandler</span><span class="p">)</span>
<span class="n">fh</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/rfc1001.txt&#39;</span><span class="p">)</span>

<span class="c1"># Process fh like a file-like object and then close it.</span>
<span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="c1"># For paths/files with unicode characters, simply pass in the URL as an unicode string</span>
<span class="n">fh2</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="sa">u</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/测试文件夹/垃圾文件.dat&#39;</span><span class="p">)</span>

<span class="c1"># Process fh2 like a file-like object and then close it.</span>
<span class="n">fh2</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>The following code snippet illustrates file upload with Python 2. You need to provide a file-like object for the <em>data</em> parameter in the <em>open()</em> method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib2</span>
<span class="kn">from</span> <span class="nn">smb.SMBHandler</span> <span class="kn">import</span> <span class="n">SMBHandler</span>

<span class="n">file_fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;local_file.dat&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>

<span class="n">director</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">SMBHandler</span><span class="p">)</span>
<span class="n">fh</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/upload_file.dat&#39;</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">file_fh</span><span class="p">)</span>

<span class="c1"># Reading from fh will only return an empty string</span>
<span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>The following code snippet illustrates file retrieval with Python 3.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib</span>
<span class="kn">from</span> <span class="nn">smb.SMBHandler</span> <span class="kn">import</span> <span class="n">SMBHandler</span>

<span class="n">director</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">SMBHandler</span><span class="p">)</span>
<span class="n">fh</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/rfc1001.txt&#39;</span><span class="p">)</span>

<span class="c1"># Process fh like a file-like object and then close it.</span>
<span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="c1"># For paths/files with unicode characters, simply pass in the URL as an unicode string</span>
<span class="n">fh2</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="sa">u</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/测试文件夹/垃圾文件.dat&#39;</span><span class="p">)</span>

<span class="c1"># Process fh2 like a file-like object and then close it.</span>
<span class="n">fh2</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>The following code snippet illustrates file upload with Python 3. You need to provide a file-like object for the <em>data</em> parameter in the <em>open()</em> method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib</span>
<span class="kn">from</span> <span class="nn">smb.SMBHandler</span> <span class="kn">import</span> <span class="n">SMBHandler</span>

<span class="n">file_fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;local_file.dat&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>

<span class="n">director</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">SMBHandler</span><span class="p">)</span>
<span class="n">fh</span> <span class="o">=</span> <span class="n">director</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;smb://myuserID:mypassword@192.168.1.1/sharedfolder/upload_file.dat&#39;</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">file_fh</span><span class="p">)</span>

<span class="c1"># Reading from fh will only return an empty string</span>
<span class="n">fh</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3><a href="../index.html">Table of Contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">SMbHandler Class</a><ul>
<li><a class="reference internal" href="#notes">Notes</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="smb_SMBConnection.html"
                          title="previous chapter">SMBConnection Class</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="smb_SMBProtocolFactory.html"
                          title="next chapter">SMBProtocolFactory Class</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/api/smb_SMBHandler.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="smb_SMBProtocolFactory.html" title="SMBProtocolFactory Class"
             >next</a> |</li>
        <li class="right" >
          <a href="smb_SMBConnection.html" title="SMBConnection Class"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">pysmb 1.2.8 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">SMbHandler Class</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2001-2021, Michael Teo https://miketeo.net/.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.0.1.
    </div>
  </body>
</html>